package org.calzz.mvc;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.calzz.jajb.JsonSerializer;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.web.servlet.view.AbstractView;

/**
 * View which allows Calzz library to be integrated with Spring MVC
 * Web Framework in a very simple way - as one of the views. In this
 * way the results will be processed by <code>JsonSerializer</code>
 * and returned as valid JSON objects.
 * 
 * @author Marek Brodziak <marek.brodziak@gmail.com>
 * @author Piotr Pejas <piotr.pejas@gmail.com>
 * @since Mar 2, 2008
 */
public class JsonView extends AbstractView {

	private static final String DEFAULT_JSON_CONTENT_TYPE = "application/json";

	private JsonSerializer serializer;

	@Override
	@SuppressWarnings("unchecked")
	protected void renderMergedOutputModel(Map map, HttpServletRequest request, HttpServletResponse response) throws Exception {
		response.setContentType(DEFAULT_JSON_CONTENT_TYPE);
		response.getWriter().append(serializer.serialize(map));
	}

	@Required
	public void setSerializer(JsonSerializer serializer) {
		this.serializer = serializer;
	}

}
